//
//  CollectionViewAndLabelReusableView.m
//  14-CollectionViewBanTang
//
//  Created by 曹曹 on 16/8/2.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "CollectionViewAndLabelReusableView.h"

#define kCollectionViewReuse @"reuseTop"

@interface CollectionViewAndLabelReusableView () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UILabel *label;
@end

@implementation CollectionViewAndLabelReusableView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // label
        _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 50)];
        [self addSubview:_label];
        _label.text = @"分类";
        // flowLayout
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, CGRectGetWidth(frame), CGRectGetHeight(frame) - 50) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        CGFloat itemHeight = (CGRectGetHeight(frame) - 50 - 40) / 3;
        layout.itemSize = CGSizeMake((CGRectGetWidth(frame) - 30) / 2, itemHeight);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        [self addSubview:_collectionView];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReuse];
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 20;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    return cell;
}

@end
